<?php

namespace app\admin\controller\statistics;

use app\common\controller\AdminController;
use app\models\user\UserGroup;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;

use app\admin\model\website\UserGroup as UserGroupModel;
/**
 * @ControllerAnnotation(title="online_record")
 */
class OnlineRecord extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\OnlineRecord();

    }

    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            foreach ($where as &$value) {
                if ($value[0] == "duration") {
                    if ($value[2] == '120') {
                        $value[1] = "<=";
                    }else if ($value[2] == "300") {
                        $value[1] = ">=";
                    }else if ($value[2] == "1800") {
                        $value[1] = ">=";
                    }
                }
            }
            
            $countModel = $this->model->where($where);
            
            if (session("admin.group") != 1 && session("admin.group") != '5') {
                $countModel->where('rid', 'in', explode(",", session("admin.rooms")));
            } else if (session('admin.group') == 5) {
                $countModel->where('rid', 'in', explode(",", session("admin.rooms")));
            }
            $count = $countModel->count();

            $listModel = $this->model->where($where);
            if (session("admin.group") != 1 && session("admin.group") != '5') {
                $listModel->where('rid', 'in', explode(",", session("admin.rooms")));
            } else if (session('admin.group') == 5) {
                $listModel->where('rid', 'in', explode(",", session("admin.rooms")));
            }
            $list = $listModel
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->toArray();


            $groups = UserGroup::column('name', 'id');

            foreach ($list as &$item) {
                $item['start_time'] = date('Y-m-d H:i', $item['start_time']);
                if ($item['end_time']) {
                    $item['end_time'] = date('Y-m-d H:i', $item['end_time']);
                } else {
                    $item['end_time'] = '---';
                }
                if ($item['ip']) {
                    if ($item['ip'] == '0.0.0.0') {
                        $item['ip'] = ' -- ';
                    } else {
                        $item['ip'] = getIp($item['ip']);
                    }
                } else {
                    $item['ip'] = ' -- ';
                }

                if (empty($item['home'])) {
                    $item['home'] = " -- ";
                }

                if (isset($groups[$item['group']])) {
                    $item['group'] = $groups[$item['group']];
                }


                if ($item['duration']) {
                    $item['duration'] = changeTimeType($item['duration']);
                } else {
                    $item['duration'] = '---';
                }




            }
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }


    /**
     * 获取用户信息
     */
    public function auth()
    {
        $rules = "1";
        
        $groups = UserGroupModel::where('id', 'not in', $rules)->select()->toArray();
        $groupList = array();
        foreach ($groups as $value) {
            $groupList[$value['id']] = $value['name'];
        }

        return json($groupList);
    }


    public function execlxls () 
    {
        list($page, $limit, $where) = $this->buildTableParames();
        foreach ($where as &$value) {
            if ($value[0] == "duration") {
                if ($value[2] == '120') {
                    $value[1] = "<=";
                }else if ($value[2] == "300") {
                    $value[1] = ">=";
                }else if ($value[2] == "1800") {
                    $value[1] = ">=";
                }
            }
        }

        $tableName = $this->model->getName();
        $tableName = CommonTool::humpToLine(lcfirst($tableName));
        
        $prefix = config('database.connections.mysql.prefix');
        $dbList = Db::connect(session("website"))->query("show full columns from {$prefix}{$tableName}");
        $header = [];
        foreach ($dbList as $vo) {
            $comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
            if ($comment != "id") {
                if (!in_array($vo['Field'], $this->noExportFields)) {
                    $header[] = [$comment, $vo['Field']];
                }
            }
           
        }
        $listModel = $this->model->where($where);
        if (session("admin.group") != 1 && session("admin.group") != '5') {
            $listModel->where('rid', 'in', explode(",", session("admin.rooms")));
        } else if (session('admin.group') == 5) {
            $listModel->where('rid', 'in', explode(",", session("admin.rooms")));
        }
        $groups = UserGroup::column('name', 'id');
        $list = $listModel
                ->order($this->sort)
                ->select()->hidden(['id'])->toArray();


        foreach ($list as &$item) {
            $item['start_time'] = date('Y-m-d H:i', $item['start_time']);
                if ($item['end_time']) {
                    $item['end_time'] = date('Y-m-d H:i', $item['end_time']);
                } else {
                    $item['end_time'] = '---';
                }
                if ($item['ip']) {
                    if ($item['ip'] == '0.0.0.0') {
                        $item['ip'] = ' -- ';
                    } else {
                        $item['ip'] = getIp($item['ip']);
                    }
                } else {
                    $item['ip'] = ' -- ';
                }

                if (empty($item['home'])) {
                    $item['home'] = " -- ";
                }

                if (isset($groups[$item['group']])) {
                    $item['group'] = $groups[$item['group']];
                }


                if ($item['duration']) {
                    $item['duration'] = changeTimeType($item['duration']);
                } else {
                    $item['duration'] = '---';
                }

        }
        $fileName = time();
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }


}